package com.freewater.javabase.demo.designpattern.state;

/**
 * Context
 */
public class CandyMachine {

    private State state;

    public final State initState;

    public final State onReadyState;

    public final State hasCoinState;

    public final State soldState;

    public final State winnerState;

    private int count = 0;

    public CandyMachine(int count) {
        this.count = count;
        this.initState = new InitState(this);
        this.hasCoinState = new HasCoinState(this);
        this.onReadyState = new OnReadyState(this);
        this.soldState = new SoldSate(this);
        this.winnerState = new WinnerState(this);
        if (count > 0) {
            this.state = onReadyState;
        } else {
            this.state = initState;
        }
    }

    public void setState(State state) {
        this.state = state;
    }

    public void insertCoin() {
        this.state.insertCoin();
    }

    public void turnCrank() {
        this.state.turnCrank();
    }

    public void returnCoin() {
        this.state.returnCoin();
    }

    public void dispense() {
        this.state.dispense();
    }

    public void releaseCandy() {
        if (count > 0) {
            count = count - 1;
            System.out.println("a candy rolling out!");
        }
    }

    public int getCount() {
        return this.count;
    }

    public void printState() {
        this.state.printState();
    }
}